%% Author: ltp-user
%% Created: 2009-10-20
%% Description: TODO: Add description to concurrency
-module(concurrency).

%%
%% Include files
%%

%%
%% Exported Functions
%%
-export([sumuj/3, licz3/2, licz2/2, licz1/2, oblicz/1]).

%%
%% API Functions
%%

%%
%% TODO: Add description of sumuj/function_arity
%%
sumuj(0, Suma, X0) ->
	io:format("#~w#Zakonczono obliczenia: 2*sin(~w) + (cos(~w^3)*exp(~w^4))^2/~w + sqrt(~w^3)=~w~n", [X0,X0,X0,X0,X0,X0,Suma]);
sumuj(N, Suma, X0) -> 
	receive
		{cz1,X}->
			io:format("Otrzymano dane od czlon1: Suma=~w N=~w ~n", [Suma, N]),
			sumuj(N-1,Suma+X, X0);
		{cz2,X}->
			io:format("Otrzymano dane od czlon2: Suma=~w N=~w ~n", [Suma, N]),
			sumuj(N-1,Suma+X, X0);
		{cz3,X} ->
			io:format("Otrzymano dane od czlon3: Suma=~w N=~w ~n", [Suma, N]),
			sumuj(N-1,Suma+X, X0)
	end.
%%
%% TODO: Add description of czlon3/function_arity
%%
licz3(X,Pid_Sumator) -> 
	io:format("Obliczanie czlon3: sqrt(~w^3)~n", [X]),
	Pid_Sumator ! {cz3,math:sqrt(math:pow(X, 3))}.
	

%%
%% TODO: Add description of czlon2/function_arity
%%
licz2(X,Pid_Sumator) -> 
	io:format("Obliczanie czlon2: (cos(~w^3)*exp(~w^4))^2/~w ~n", [X,X,X]),
	Pid_Sumator ! {cz2,(math:cos(math:pow(X, 3)*math:exp(math:pow(X, 4))))/X}.
	
%%
%% TODO: Add description of czlon1/function_arity
%%
licz1(X,Pid_Sumator) ->
    io:format("Obliczanie czlon1:2*sin(~w)~n", [X]),
	Pid_Sumator !{cz1,2*math:sin(X)}.
	
%%
%% TODO: Add description of oblicz/function_arity
%%
oblicz([])->
	io:format("Koniec listy danych",[]);
oblicz([First|Rest]) ->
	oblicz(First),
	oblicz(Rest);
oblicz(X) -> 
	io:format("#~w#Obliczanie: 2*sin(~w) + (cos(~w^3)*exp(~w^4))^2/~w + sqrt(~w^3)~n", [X,X,X,X,X,X]),
	Pid_Sumator=spawn(concurrency, sumuj, [3,0,X]),
	spawn(concurrency, licz1, [X,Pid_Sumator]),
	spawn(concurrency, licz2, [X,Pid_Sumator]),
	spawn(concurrency, licz3, [X,Pid_Sumator]),
	ok.


%%
%% Local Functions
%%

